<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>MatchVSDemo</title>
    <script src="../../server/src/gl/java/javascript/script.js"></script>
</head>
<body>
<form onsubmit="return false;">
    <label for="responseText">
        <textarea id="responseText" style="width: 1280px; height: 300px;"
                  disabled="disabled" readonly="readonly"></textarea>
    </label>


    <br>-------服务器环境---------<br>
    <input type="button" value="Dev" onclick="changedEnvir('Dev')">
    <input type="button" value="HeXianMaJiang" onclick="changedEnvir('HeXianMaJiang')">
    <input type="button" value="LocalConf" onclick="changedEnvir('LocalConf')">
    <input type="button" value="fluttergo" onclick="changedEnvir('fluttergo')">
    <br>-----------------游戏配置-------------------------------------------<br>
    gameID <input type="text" name="gameID" id="gameID" value="0">
    frameRate <input type="text" name="frameRate" id="frameRate" placeholder="default 10 frame per second" value="10">
    <br>-------用户信息-----------------------------------------<br>
    <select name="userID" id="userID">
        <option value="524551">524551</option>
        <option value="524550">524550</option>
        <option value="524549">524549</option>
        <option value="524548">524548</option>
    </select>
    token <input type="text" name="token" id="token" value="xyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxy">
    <br>


    <br>-------Quick Start--------<br>
    <!--<input type="button" value="clearUser" onclick="clearUser()">-->
    <input type="button" value="login" onclick="login()">

    <!--<input type="button" value="setFrameSync" onclick="setFrameSync()">-->
    <!--<input type="button" value="sendFrameEvent" onclick="sendFrameEvent()">-->
    <!--<br>-->

    <br>-------Room---------------------------<br>
    <!--<input type="button" value="getRoomList" onclick="getRoomList()">-->

    <input type="button" value="match" onclick="match()">
    <input type="button" value="joinOver" onclick="stopJoin()">

    <br>------Data---------<br>
    <input type="text" name="chat" id="chat" placeholder="hello world" value="10">
    <input type="button" value="broadcast" onclick="broadcast()">

    <br>-------Out---------<br>
    <input type="button" value="disConnect" onclick="disConnect()">


</form>

</body>

<script type="text/javascript" src="md5.js"></script>
<script type="text/javascript" src="format.js"></script>
<script type="text/javascript" src="msutil.js"></script>
<script type="text/javascript" src="base64.js"></script>
<script type="text/javascript" src="uiutil.js"></script>
<script type="text/javascript" src="config.js"></script>
<script type="text/javascript" src="websocketclient.js"></script>
<script type="text/javascript" src="umspdefine.js"></script>
<script type="text/javascript" src="umspprotocol.js"></script>
<script type="text/javascript" src="umspclient.js"></script>
<script type="text/javascript">

    function clearUser() {
        LocalStore_Clear();
    }

    var isBenchmark = false;

    var engine = new UmspClient();
    var envir = "LocalConf";

    printLog("=============================================================");
    printLog("=========== " + engine.getVersion() + " =====================");
    printLog("================ Envir: " + envir + " =======================");
    printLog("=============================================================");


    var login = function () {
        printLog("login To ");

        engine.login(vt("userID"), vt("token"), vt("gameID"), function (rsp) {
            printLog("[Rsp]login.status:" + utf8ByteArrayToString(rsp.payload));
            //loading
        }, {
            onConnect: function (host) {
                printLog("[NetWork] [Connect success]: host:" + host + "] ");
            },
            onErr: function (errCode, errMsg) {
                printLog("[NetWork] [onErr]:[" + errCode + "] errMsg:" + errMsg);
            },
            onDisConnect: function (errCode, errMsg, host) {
                printLog("[NetWork] [disConnect] host:" + host + ", errCode:" + errCode + " errMsg:" + errMsg);
            }
        }, envir);

        printLog("userID: " + "," + vt("userID") + ",token:" + vt("token") + ",gameID:"
            + vt("gameID"));
    };


    var match = function () {
        var matchOption = new Match(Match.prototype.SAME_NAME);
        matchOption.maxUserCount = 3;
        matchOption.roomName = "开房时";
        engine.match(function (rsp) {
                var json = JSON.parse(utf8ByteArrayToString(rsp.payload));
                if (json["isSuccess"] === true) {
                    var room = json["room"];
                    printLog("=========================================================");
                    printLog("[Rsp]matchRoomID:" + room["roomID"]);
                    printLog("=========================================================");
                    printLog("                                                         ");
                } else {
                    printLog("[W] match fail");
                }


            },
            function (rsp) {
                var userChanged = JSON.parse(utf8ByteArrayToString(rsp.payload));
                printLog("[Rsp]room userID:" + userChanged["userID"] + " changed :" + userChanged["userAction"]);
                var currentUserList = userChanged["currentUserList"];
                for (var i = 0; i < currentUserList.length; i++) {
                    printLog("[Rsp]room UserList["+i+"]:" + currentUserList[i]["userID"]);

                }
            }, {
                onConnect: function (host) {
                    printLog("[NetWork] [Connect success]: host:" + host + "] ");
                },
                onErr: function (errCode, errMsg, host) {
                    printLog("[NetWork] [onErr]:[" + errCode + "] errMsg:" + errMsg);
                },
                onDisConnect: function (errCode, errMsg, host) {
                    printLog("[NetWork] [disConnect] host:" + host + ", errCode:" + errCode + " errMsg:" + errMsg);
                }
            },matchOption);
        printLog("matchOption-> "+JSON.stringify(matchOption));
    };

    var broadcast = function () {

        var data = vt("chat");
        engine.broadcast(data, function (msg) {
            printLog(msg)
        });
        if (!isBenchmark) {
            printLog("broadcast-> " + data);
        } else {
            printLog("broadcast-> " + data);
        }
    };


    var stopJoin = function () {
        engine.stopJoin();
        printLog("stopJoin");

    };


    var getRoomList = function () {
        var par = new MsRoomFilter(2, 0, 0, null);
        engine.getRoomList(par);
    };

    var disConnect = function () {
        engine.disConnect();
        printLog("disConnect");
    };
    var changedEnvir = function (vir) {
        envir = vir;
        printLog("chanegd envir to :" + envir);
    };


    var benchMarkTimeOut;
    var benchMark = function () {
        isBenchmark = !isBenchmark;
        if (isBenchmark) {
            benchMarkTimeOut = setInterval(function () {
                broadcast();
            }, 16);
            printLog("benchMark is " + isBenchmark);
        } else {
            clearInterval(benchMarkTimeOut);
            printLog("benchMark is " + isBenchmark);
        }
    };

</script>


</html>


